//logs.js
var formatFloat = require('../../utils/mathUtil.js')
var app = getApp()
var host = app.globalData.host
Page({
  data: {
    openId: '',
    shopcarcontent: '',
    selectedStatus: '',
    image: app.globalData.image,
    totalNum: 0,
    totalPrice: 0.0,
    shopcarempty: '',
    shopcarunempty: '',
    left: 0,
    leftindex: ''
  },
  refresh: function (e) {
    var that = this

    wx.showLoading({
      title: '',
    })
    wx.request({
      url: host + '/cart/list',
      header: { 'content-type': 'application/json' },
      data: {
        openId: wx.getStorageSync('openId')
      },
      success: function (res) {
        wx.hideLoading()
        var list = res.data.data.list
        var allNum = 0
        var allPrice = 0.0
        var selectedStatus = 'checked'

        for (var z = 0; z < list.length; z++) {
          if (list[z].note == "checked") {
            allNum += list[z].skuNum
            // console.info("添加之前总价:" + allPrice);
            // console.info("这批商品信息");
            // console.info("数量:" + list[z].skuNum);
            // console.info("单价:" + list[z].sku.price);
            // console.info("计算的总价:" + list[z].sku.price * list[z].skuNum);
            allPrice += (list[z].sku.price * list[z].skuNum)
            // console.info("添加之后总价:" + allPrice);
            allPrice = formatFloat.formatFloat(allPrice, 2)
            // console.info("处理之后总价:" + allPrice);
          } else {
            selectedStatus = ''
          }

        }
        that.setData({
          shopcarcontent: list,
          totalNum: allNum,
          selectedStatus: selectedStatus,
          totalPrice: allPrice
        })
        if (that.data.shopcarcontent.length > 0) {
          that.setData({
            shopcarempty: false,
            shopcarunempty: true
          })
        } else {
          that.setData({
            shopcarempty: true,
            shopcarunempty: false
          })
        }
      }
    })//request
  },
  changeStatus: function (e) {//更改某个checkbox触法的操作
    // console.log(e)
    // console.log(e.currentTarget.dataset.id)
    var that = this
    wx.request({
      url: host + '/cart/update',
      header: { 'content-type': 'application/json' },
      data: {
        openId: wx.getStorageSync('openId'),
        skuId: e.currentTarget.dataset.id,
        event: 'toggleSelectStatus'
      },
      success: function (err_code) {
        that.refresh()
      }
    })
  },//changeStatus
  onShow: function () {
    var that = this;
    that.refresh()
  },
  selectProduct: function (e) {
    var id = e.currentTarget.dataset.id;
    var url = '../wedproductdetail/wedproductdetail?id=' + id
    wx.navigateTo({
      url: url
    }) 
  },
  add: function (e) {
    var that = this
    var skuid = e.currentTarget.dataset.skuid;
    var id = e.currentTarget.dataset.id;
    var num = e.currentTarget.dataset.num;
    var index = e.currentTarget.dataset.index;
    var value = e.currentTarget.dataset.value;
    if (value == "1") num = num + 1;
    else {
      if (num == 1) return;
      else num = num - 1;
    }

    wx.request({
      url: host + '/cart/update',
      header: { 'content-type': 'application/json' },
      data: {
        openId: wx.getStorageSync('openId'),
        skuId: skuid,
        productNum: num
      },
      success: function (res) {
        that.refresh()
      }
    })

  },
  selectAll: function (e) {
    var that = this
    var resultStatus = ''
    var selectedStatus = that.data.selectedStatus;
    // console.log("selectedStatus = " + selectedStatus);
    if (selectedStatus == 'checked') resultStatus = 0
    else resultStatus = 1

    that.setData({
      selectedStatus: resultStatus
    })
    // console.log("resultStatus = " + resultStatus);
    wx.request({
      url: host + '/cart/updateAll',
      header: { 'content-type': 'application/json' },
      data: {
        openId: wx.getStorageSync('openId'),
        status: resultStatus
      },
      success: function (res) {
        that.refresh()
      }
    })
  },
  topay: function (e) {
    var that = this
    var totalNum = that.data.totalNum

    if (totalNum < 1) {
      wx.showToast({
        title: '请先选择商品',
        icon: 'success',
        duration: 2000
      })
    } else {
      wx.navigateTo({
        url: '../ordersure/ordersure'
      })
    }
  },
  deleteFromCart: function (e) {
    var that = this
    var skuid = e.currentTarget.dataset.skuid;
    wx.request({
      url: host + '/cart/delete',
      header: { 'content-type': 'application/json' },
      data: {
        openId: wx.getStorageSync('openId'),
        skuId: skuid
      },
      success: function (err_code) {
        that.refresh()
      }
    })
  },
  // 左划删除
  touchS: function (e) {
    console.log(e)
    this.setData({
      leftindex: e.currentTarget.dataset.index
    })
    if (e.touches.length == 1) {
      this.setData({
        startX: e.touches[0].clientX
      })
    }
  },
  touchM: function (e) {
    var that = this;
    if (e.touches.length == 1) {
      var moveX = e.touches[0].clientX;
      //记录触摸点位置的X坐标
      var moveX = e.touches[0].clientX;
      //计算手指起始点的X坐标与当前触摸点的X坐标的差值
      var disX = that.data.startX - moveX;
      if (disX == 0 || disX < 0) {//如果移动距离小于等于0，文本层位置不变
        that.setData({
          left: 0
        })
      } else if (disX > 0 && disX < 109) {//移动距离大于0，文本层left值等于手指移动距离
        that.setData({
          left: "-" + disX + "rpx"
        })
        if (disX >= 109) {
          //控制手指移动距离最大值为删除按钮的宽度
          that.setData({
            left: "-109rpx"
          })
        }
      }
    }
  },
  touchE: function (e) {
    var that = this;
    if (e.changedTouches.length == 1) {
      //手指移动结束后触摸点位置的X坐标
      var endX = e.changedTouches[0].clientX;
      //触摸开始与结束，手指移动的距离
      var disX = that.data.startX - endX;
      var delBtnWidth = that.data.delBtnWidth;
      //如果距离小于删除按钮的1/2，不显示删除按钮
      var left = disX > 55 ? "-109rpx" : "0";
      that.setData({
        left: left
      })
    }
  },
  onHide: function () {
    this.setData({
      left: 0
    })
  },
  // 回到首页
  goindex: function () {
    wx.switchTab({
      url: '../index/index',
    })
  }
})